﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Events;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.ServiceLocation;
using Douban.Core.Events;
using Douban.Core.Messages;
using Douban.Core;
using Douban.Api.Mail;
using System.Windows.Threading;

namespace Douban
{
    public class NotifyIconViewModel : ObservableObject
    {
        private IEventAggregator _eventAggregator;
        private IShellView _shellView;
        private IMessageStack _messageStack;
        private ICommand _closeCommand;
        private ICommand _notifyIconDoubleClickCommand;
        private ICommand _openSettingWindowCommand;
        private Message _latestMessage;
        private DispatcherTimer _notifyTimer;
        private string _iconUri;
        private string _toolTipText;
        private const string DefaultToolTipText = "豆Q";
        private const double flashFrequency = 500;
        private const string DefaultIconUri = "/Images/douban.ico";

        public NotifyIconViewModel(
            IEventAggregator eventAggregator, 
            IShellView shellView,
            IMessageStack messageStack)
        {
            _eventAggregator = eventAggregator;
            _shellView = shellView;
            _messageStack = messageStack;
            _eventAggregator.GetEvent<MessageArrivedEvent>().Subscribe(OnMessageArrived);

            _messageStack.MessageRetrieved += (sender, e) => ResetIcon();
            ResetToolTipText();
            ResetIcon();
        }

        public string IconUri
        {
            get { return _iconUri; }
            set
            {
                if (_iconUri != value)
                {
                    _iconUri = value;
                    NotifyPropertyChanged("IconUri");
                }
            }
        }
     
        public string ToolTipText
        {
            get { return _toolTipText; }
            set
            {
                if (_toolTipText != value)
                {
                    _toolTipText = value;
                    NotifyPropertyChanged("ToolTipText");
                }
            }
        }
				
        public ICommand OpenSettingWindowCommand
        {
            get
            {
                if (_openSettingWindowCommand == null)
                    _openSettingWindowCommand = new DelegateCommand<object>(
                        obj =>
                        {
                            if (!SettingWindowHelper.IsOpen)
                            {
                                SettingWindowHelper.IsOpen = true;
                                ServiceLocator.Current.GetInstance<ISettingViewModel>().Open();
                            }
                        });

                return _openSettingWindowCommand;
            }
        }

        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new DelegateCommand<object>(obj => _shellView.CloseView());

                return _closeCommand;
            }
        }

        public ICommand DoubleClickCommand
        {
            get
            {
                if (_notifyIconDoubleClickCommand == null)
                {
                    _notifyIconDoubleClickCommand = new DelegateCommand<object>(
                        obj =>
                        {
                            if (!_messageStack.HasMessages())
                            {
                                _shellView.Toggle();
                            }
                            else
                            {
                                _messageStack.PopMessage();
                            }
                        });
                }

                return _notifyIconDoubleClickCommand;
            }
        }

        private void OnMessageArrived(Message message)
        {
            _latestMessage = message;
            if (_notifyTimer == null)
            {
                _notifyTimer = new DispatcherTimer();
                _notifyTimer.Interval = TimeSpan.FromMilliseconds(flashFrequency);
                _notifyTimer.Tick += new EventHandler(_notifyTimer_Tick);
                _notifyTimer.Start();
            }
            
        }

        private void _notifyTimer_Tick(object sender, EventArgs e)
        {
            if (!_messageStack.HasMessages())
            {
                _notifyTimer.Stop();
                _notifyTimer.Tick -= _notifyTimer_Tick;
                _notifyTimer = null;
                ResetToolTipText();
                return;
            }

            ToolTipText = _latestMessage.Sender.Name;

            string iconLocalPath = NotifyIconHelper.GetLocalIcon(
                _latestMessage.Sender.Id, _latestMessage.Sender.IconUri);

            if (IconUri == null)
                ResetIcon();
            else
                IconUri = null;
        }

        private void ResetIcon()
        {
            IconUri = DefaultIconUri;
        }

        private void ResetToolTipText()
        {
            ToolTipText = DefaultToolTipText;
        }
    }
}
